/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GameLogic;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map.Entry;
import javax.swing.text.html.parser.Entity;


/**
 *
 * @author Ix
 */
public class Buff implements Comparable<Buff>
{
    public String Name;
    public String Tooltip;
    public final static int AURA = 420420;
    public double MaxLifeMod;
    public double MoveSpeedMod;
    public double ArmorMod;
    public ArmorType ArmorTypeMod;
    public boolean isFlyingMod;
    public int Duration;
    
    @Override
    public boolean equals(Object o)
    {
        return (Name == null ? ((Buff)o).Name == null : Name.equals(((Buff)o).Name));
    }

    @Override
    public int hashCode() {
        return Name.hashCode();
    }
    @Override
    public int compareTo(Buff b)
    {
        return Name.compareTo(b.Name);
    }
    
    public Buff(String name, int Duration, double lifeModPercent, double msModPercent, double armorMod, boolean removeFlying)
    {
        Name = name;
        MaxLifeMod = lifeModPercent;
        MoveSpeedMod = msModPercent;
        ArmorMod = armorMod;
        isFlyingMod = removeFlying;
        this.Duration = Duration;
    }
    
    public static void UpdateBuffs(Mob m, int ms)
    {
        //m.Buffs.
        ArrayList<Buff> toRem = new ArrayList<Buff>();
        for(Entry<Buff, Integer> e : m.Buffs.entrySet())
        {
            if(e.getValue() != AURA)
            {
                e.setValue(e.getValue() - (Integer)ms);
                if(e.getValue() <= 0)
                    toRem.add(e.getKey());
            }
        }
        for(Buff b : toRem)
        {
            b.Remove(m);
        }
    }
    
    public void Apply(Mob m)
    {
        if(m.Buffs.containsKey(this))
        {
            Integer i = m.Buffs.get(this);
            if(i < Duration)
                m.Buffs.put(this, Duration);
        }
        else
        {
            System.out.println("Added buff");
            m.Buffs.put(this, Duration);
            
            apply_effect(m, true);
        }
    }
    private void apply_effect(Mob m, boolean add)
    {
        int i = -1;
        if(add)
            i = 1;
        //life
        if(MaxLifeMod != 0)
        {
            double lp = m.CurLife / m.MaxLife,
                cph = m.MaxLife / m.type.MaxLife;
            cph += i * MaxLifeMod;
            m.MaxLife = (int)((double)m.type.MaxLife * cph);
            m.CurLife = (int)((double)m.MaxLife * lp);
        }
        //movespeed
        if(MoveSpeedMod != 0)
        {
            double cp = m.MoveSpeed / m.type.MoveSpeed;
            cp += i * MoveSpeedMod;
            m.MoveSpeed = m.type.MoveSpeed * cp;
        }
        //armor
        m.Armor += i * ArmorMod;
    }
    
    public void Remove(Mob m)
    {
        if(!m.Buffs.containsKey(this))
            return;
        //REMOVE
        m.Buffs.remove(this);
        System.out.println("Removed buff");
        apply_effect(m, false);
    }
    
}
